﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Evolution.Structure.Globe.Details.Cities;
using System.ComponentModel;
using Evolution.Structure.Globe.Fields;
using Evolution.Structure.Globe.Details;
using Evolution.Structure;
using System.Windows.Input;
using EvolutionBaseTypes.Interfaces;
using Evolution.Structure.Globe.Units;

namespace GameProject.ViewModel
{
    class CityViewViewModel : ViewModelBase
    {
        public BindingList<IBuildable> ConstructionQueue
        {
            get;
            private set;
        }

        public ICommand BuildBuildingCommand
        {
            get
            {
                if (_BuildBuildingCommand == null)
                {
                    _BuildBuildingCommand = new RelayCommand(param => buildBuilding(), param => canBuildBuilding());
                }
                return _BuildBuildingCommand;
            }
        }
        private ICommand _BuildBuildingCommand;

        private bool canBuildBuilding()
        {
            if (SelectedOptionalBuilding != null)
                return true;
            return false;
        }


        private void buildBuilding()
        {
            SelectedCity.ConstructingQueue.Enqueue(SelectedOptionalBuilding);
            ConstructionQueue.Add(SelectedOptionalBuilding);
            OptionalBuildings.Remove(SelectedOptionalBuilding);
        }


        public Player CurrentPlayer
        {
            get { return GlobalVar.CurrentPlayer; }
        }

        public double MoneyPoints
        {
            get { return GlobalVar.CurrentPlayer.PlayerResources.MoneyPoints.Value; }
        }
        
        public int CityMapSize
        {
            get { return Constants.CITYMAPSIZE; }
        }
        
        public BindingList<Field> WorldFields
        {
            get { return _WorldFields; }
        }
        private BindingList<Field> _WorldFields;

        public CityViewViewModel(Evolution.Structure.Globe.Details.Cities.City city)
        {
            SelectedCity = city;
            _WorldFields = GlobalVar.World.getWorldFieldList(city.Position, Constants.CITYMAPSIZE, false);
            checkOptionalBuildings();
            checkTrainableUnits();

        }
        #region OptionalBuilding

        private void checkOptionalBuildings()
        {
                        ConstructionQueue = new BindingList<IBuildable>();
                        foreach (IBuildable buildable in SelectedCity.ConstructingQueue)
            {
                ConstructionQueue.Add(buildable);
            }

            OptionalBuildings = new BindingList<Building>();
            foreach (Building building in GlobalVar.Encyclopdia.BuildingList)
            {
                if (!SelectedCity.Buildings.Contains(building) && !SelectedCity.ConstructingQueue.Contains(building))
                    OptionalBuildings.Add(building);
            }
        }


        public BindingList<Building> OptionalBuildings
        {
            get;
            private set;
        }

        public Building SelectedOptionalBuilding
        {
            get { return _SelectedOptionalBuilding; }
            set { _SelectedOptionalBuilding = value;
            base.onPropertyChanged("SelectedOptionalBuilding");
            }
        }
        private Building _SelectedOptionalBuilding;

        #endregion OptionalBuilding

        #region Unit-Training

        public BindingList<Unit> TrainableUnits
        {
            get;
            private set;
        }

        private void checkTrainableUnits()
        {
            TrainableUnits = new BindingList<Unit>();
            foreach (Unit Unit in GlobalVar.Encyclopdia.UnitList)
            {
                TrainableUnits.Add(Unit);
            }
        }

        public Unit SelectedTrainableUnit
        {
            get { return _SelectedTrainableUnit; }
            set
            {
                _SelectedTrainableUnit = value;
                base.onPropertyChanged("SelectedTrainableUnit");
            }
        }
        private Unit _SelectedTrainableUnit;

        #endregion Unit-Training


        public City SelectedCity
        {
            get;
            private set;
        }
    }
}
